package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.client.Veilingsite;
import hu.veilingsite.shared.Auction;
import hu.veilingsite.shared.Category;
import hu.veilingsite.shared.FieldVerifier;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;

public class ProfileAuctions {
	private DatePicker datePicker;
	private ListBox LB_category;
	private Button BTN_addAuction;
	private TextArea TA_description;
	private TextBox TB_title, TB_credits;
	private VerticalPanel addAuctionPanel;
	private ArrayList<Category> categories;
	final ServiceAsync myService = (ServiceAsync)GWT.create(Service.class);
	
	public ProfileAuctions() {
		if (Systeem.ingelogd == null) {
			RootPanel.get("main").add(new Veilingsite().createHome());
		} else {
			RootPanel.get("main").clear();
			addAuctionPanel = new VerticalPanel();
		
			HTML intro = new HTML();
			intro.setHTML("<h1>Voeg hier een nieuwe veiling toe</h1><br /><br />");
			addAuctionPanel.add(intro);
			
			AsyncCallback<ArrayList<Category>> getCategoriesCallback = new AsyncCallback<ArrayList<Category>>(){
				@Override
				public void onFailure(Throwable caught) {
					addAuctionPanel.clear();
					addAuctionPanel.add(new HTML("Er is een fout opgetreden in het systeem."));
				}
	
				@Override
				public void onSuccess(ArrayList<Category> result) {
					if (Systeem.ingelogd.isMayoffer()) {
						categories = result;
						datePicker = new DatePicker();
						LB_category = new ListBox();
						LB_category = new ListBox();
						for(int i = 0; i < categories.size(); i++) {
							Category cat = categories.get(i);
							LB_category.addItem(cat.getTitle(), "" + cat.getId());
						}
						BTN_addAuction = new Button("voeg toe");
						TB_title = new TextBox();
						TA_description = new TextArea();
						TA_description.setHeight("50px");
						TB_credits = new TextBox();
						
						FlexTable table = new FlexTable();
						table.addStyleName("nicetable");
					
						table.setWidget(0, 0, new Label("Categorie:"));
						table.setWidget(0, 1, LB_category);
						table.setWidget(1, 0, new Label("Titel:"));
						table.setWidget(1, 1, TB_title);
						table.setWidget(2, 0, new Label("Beschrijving:"));
						table.setWidget(2, 1, TA_description);
						table.setWidget(3, 0, new Label("Aantal credits"));
						table.setWidget(3, 1, TB_credits);
						table.setWidget(4, 0, new Label("Einddatum:"));
						table.setWidget(4, 1, datePicker);
						table.setWidget(5, 1, BTN_addAuction);
						
						BTN_addAuction.addClickHandler(new ClickHandler(){
							@Override
							public void onClick(ClickEvent event) {
								int credits = Integer.parseInt(TB_credits.getText());
								
								final AsyncCallback<Boolean> createAuction = new AsyncCallback<Boolean>() {
									@Override
									public void onFailure(Throwable caught) {
										FieldVerifier.showMessageBox("Oops!", "Uw veiling kon niet worden aangemaakt, probeer het nog een keer.", true);
									}
									@Override
									public void onSuccess(Boolean result) {
										if(result) {
											RootPanel.get("main").add(new ProfileAuctions().createPage());
											FieldVerifier.showMessageBox("Succes!", "U heeft succesvol een veiling aangemaakt.", true);
										} else {
											System.out.println("De ingevulde gegevens zijn incorrect ingevuld.");
										}
									}
								};
			
								myService.createAuction(Systeem.ingelogd.getId(), Integer.parseInt(LB_category.getValue(LB_category.getSelectedIndex())), TB_title.getText(), TA_description.getText(),credits , datePicker.getValue(), 1,  createAuction);
							}
						});
					
						addAuctionPanel.add(table);
						RootPanel.get("main").clear();
						RootPanel.get("main").add(addAuctionPanel);
					} else {
						RootPanel.get("main").clear();
						RootPanel.get("main").add(new HTML("U heeft geen rechten om veilingen aan te bieden."));
					}
				
					AsyncCallback<ArrayList<Auction>> getAllAuctionsFromUser = new AsyncCallback<ArrayList<Auction>>(){
			
						@Override
						public void onFailure(Throwable caught) {
							RootPanel.get("main").clear();
							RootPanel.get("main").add(new HTML("Er is een fout opgetreden in het systeem."));
						}
			
						@Override
						public void onSuccess(ArrayList<Auction> result) {
							if (result.size() > 0) {
								FlexTable veilingenPanel = new FlexTable();
								veilingenPanel.setWidget(0,0, new HTML("<h1>Uw veilingen</h1>"));
								int row = 0;
								for(int i = 0; i < result.size(); i++){
									final Auction auction = result.get(i);
									Date until = new Date(auction.getEndDate().getTime());
									Date now = new Date();
									String tijdover = "";
									long timeleft = until.getTime() - now.getTime();
									if(timeleft > 0) {
										// Easy as that ;D
										long days = (long) Math.ceil(timeleft / (1000 * 60 * 60 * 24));
										timeleft = timeleft - (days * (1000 * 60 * 60 * 24));
										if(days > 0) tijdover += days + " dagen ";
										long hours = (long) Math.ceil(timeleft / (1000 * 60 * 60));
										timeleft = timeleft - (hours * (1000 * 60 * 60));
										if(hours > 0) tijdover += hours + " uur ";
										long min = (long) Math.ceil(timeleft / (1000 * 60));
										timeleft = timeleft - (min * (1000 * 60));
										if(min > 0) tijdover += min + " min ";
										long sec = (long) Math.ceil(timeleft / (1000));
										timeleft = timeleft - (sec * (1000));
										tijdover += sec + " sec.";
									}
									else{
										tijdover = "Veiling gesloten.";
									}
									veilingenPanel.setWidget(row+1,0, new HTML("<br/><br/><h3>"+ auction.getTitle() +"</h3>"));
									veilingenPanel.setWidget(row+2,0, new HTML("<b>Gestart met:</b>"));
									veilingenPanel.setWidget(row+2,1, new HTML("" + auction.getStartprice() + " credits"));
									veilingenPanel.setWidget(row+3,0, new HTML("<b>Loopt tot:</b>"));
									veilingenPanel.setWidget(row+3,1, new HTML(auction.getEndDate().toString()));
									veilingenPanel.setWidget(row+4,0, new HTML("<b>Tijd over:</b>"));
									veilingenPanel.setWidget(row+4,1, new HTML(tijdover));
									ClickHandler bekijkHandler = new ClickHandler() {
										@Override
										public void onClick(ClickEvent event) {
											RootPanel.get("main").clear();
											RootPanel.get("main").add(new ViewAuction(auction.getId()).createPage());
										}
									};
									veilingenPanel.setWidget(row+5,0, new Button("Bekijk", bekijkHandler));
									ClickHandler wijzigHandler = new ClickHandler() {
										@Override
										public void onClick(ClickEvent event) {
											RootPanel.get("main").clear();
											new EditAuction().createPage(auction.getId());
										}
									};
									veilingenPanel.setWidget(row+5,1, new Button("Wijzig", wijzigHandler));
									row += 5;
								}
								RootPanel.get("main").add(veilingenPanel);
							} else{
								RootPanel.get("main").add(new HTML("U heeft nog geen veilingen."));
							}
						}
					};
					myService.getAllAuctionsFromUser(Systeem.ingelogd.getId(), getAllAuctionsFromUser);
				}
			};
			myService.getCategories(getCategoriesCallback);
		}
		
	}
	
	public Panel createPage() {
		return new VerticalPanel();
	}
}
